package com.anolesoft.amljmes.fourteenDispatch.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.amljmes.fourteenDispatch.dto.MesProcessQlbzDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author zyq
 * @date 2022/8/25
 */
@Mapper
public interface MesProcessQlbzMapper {
    /**
     * 条件查询
     * @param mesProcessQlbzDO
     * @param page
     * @return
     */
    @SelectProvider(type = createSQL.class, method = "queryQlbzSQL")
    List<MesProcessQlbzDO> queryQlbz(@Param("mesProcessQlbzDO") MesProcessQlbzDO mesProcessQlbzDO, Page<MesProcessQlbzDO> page);
    public static class createSQL{
        public static String queryQlbzSQL(@Param("mesProcessQlbzDO") MesProcessQlbzDO mesProcessQlbzDO, Page<MesProcessQlbzDO> page){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" a.objuid, a.partid, a.gongxuid, a.gongxuname, a.qiliangtime from mes_process_qlbz a");
                    sql.append(" where a.delete_flag = 0 and a.factory_id = #{mesProcessQlbzDO.factoryId}");
                    if (!StrUtil.isEmptyOrUndefined(mesProcessQlbzDO.getPartid())){
                        sql.append(" and a.partid = trim(upper(#{mesProcessQlbzDO.partid}))");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesProcessQlbzDO.getGongxuname())){
                        sql.append(" and a.gongxuname = trim(#{mesProcessQlbzDO.gongxuname})");
                    }
                    sql.append(" order by a.partid");
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    /**
     * 查看是否与表内数据重复
     * @param mesProcessQlbzDO
     * @return
     */
    @Select("select count(*)\n" +
            "  from mes_process_qlbz a\n" +
            " where a.delete_flag = 0\n" +
            "   and a.factory_id = #{mesProcessQlbzDO.factoryId}\n" +
            "   and a.partid = trim(upper(#{mesProcessQlbzDO.partid}))\n" +
            "   and a.gongxuname = trim(#{mesProcessQlbzDO.gongxuname})\n" +
            "   and a.objuid != case\n" +
            "         when #{mesProcessQlbzDO.objuid} is null then\n" +
            "          '0'\n" +
            "         else\n" +
            "          #{mesProcessQlbzDO.objuid}\n" +
            "       end\n")
    int repeatQjbz(@Param("mesProcessQlbzDO") MesProcessQlbzDO mesProcessQlbzDO);

    /**
     * 新增
     * @param mesProcessQlbzDO
     */
    @Insert("insert into mes_process_qlbz(partid,gongxuid,gongxuname,qiliangtime,create_id,create_time,factory_id)" +
            " values (trim(upper(#{mesProcessQlbzDO.partid})),trim(#{mesProcessQlbzDO.gongxuid})," +
            " trim(#{mesProcessQlbzDO.gongxuname}),#{mesProcessQlbzDO.qiliangtime},#{mesProcessQlbzDO.createId}," +
            " sysdate,#{mesProcessQlbzDO.factoryId})")
    void insertQlbz(@Param("mesProcessQlbzDO") MesProcessQlbzDO mesProcessQlbzDO);

    /**
     * 修改
     * @param mesProcessQlbzDO
     */
    @Update("update mes_process_qlbz a set\n" +
            "a.partid = trim(upper(#{mesProcessQlbzDO.partid})),\n" +
            "a.gongxuid = trim(#{mesProcessQlbzDO.gongxuid}),\n" +
            "a.gongxuname = trim(#{mesProcessQlbzDO.gongxuname}),\n" +
            "a.qiliangtime = trim(#{mesProcessQlbzDO.qiliangtime}),\n" +
            "a.modify_id = #{mesProcessQlbzDO.modifyId},\n" +
            "a.modify_time = sysdate\n" +
            "where a.objuid = #{mesProcessQlbzDO.objuid}")
    void updateQlbz(@Param("mesProcessQlbzDO") MesProcessQlbzDO mesProcessQlbzDO);

    /**
     * 删除
     * @param mesProcessQlbzDO
     */
    @Update("update mes_process_qlbz a set\n" +
            "a.delete_flag = 1,\n" +
            "a.delete_id = #{mesProcessQlbzDO.deleteId},\n" +
            "a.delete_time = sysdate\n" +
            "where a.objuid = #{mesProcessQlbzDO.objuid}")
    void deleteQlbz(@Param("mesProcessQlbzDO") MesProcessQlbzDO mesProcessQlbzDO);
}
